clear *

use "${root}/data/processed/final_sample.dta", clear

keep if baseline_sample == 1

* generate groups by population size
gen size_above_25pct = 0 if mun_pop_avg!=.
gen size_above_median = 0 if mun_pop_avg!=.
gen size_above_75pct =0 if mun_pop_avg!=.
gen size_above_90pct =0 if mun_pop_avg!=.

sum mun_pop_avg, detail
local pop25pct = r(p25)
local pop50pct = r(p50)
local pop75pct = r(p75)
local pop90pct = r(p90)

replace size_above_25pct = 1 if mun_pop_avg > `pop25pct' & mun_pop_avg!=.
replace size_above_median = 1 if mun_pop_avg > `pop50pct' & mun_pop_avg!=.
replace size_above_75pct = 1 if mun_pop_avg > `pop75pct' & mun_pop_avg!=.
replace size_above_90pct = 1 if mun_pop_avg > `pop90pct' & mun_pop_avg!=.

* format local to use in the latex table
sum mun_pop_avg, detail
local pop25pct : di %11.0fc r(p25)
local pop50pct : di %11.0fc r(p50)
local pop75pct : di %11.0fc r(p75)
local pop90pct : di %11.0fc r(p90)

********* Perform (robust bias-corrected) RD estimates and produce results table **********

foreach outcome in tot_exp_avg_pc tot_exp_avg_sgdp tot_rev_avg_pc tot_rev_avg_sgdp current_exp_avg_share investments_avg_share personnel_avg_share social_exp_avg_share social_exp_avg_pc health_avg_share education_avg_share welfare_avg_share nonsocial_exp_avg_share housing_avg_share transport_avg_share other_avg_share {

*** (1) baseline (whole sample, average over 4-years, CCT 2014 MSE-optimal bandwidth)

	qui rdrobust res_`outcome' margin_mayor_left, vce(cluster mun_code) all

	local coeff1_`outcome' : di %9.2f (e(tau_bc))
	local se1_`outcome' : di %9.2f (e(se_tau_rb))
	local pv1_`outcome' : di %9.2f (e(pv_rb))
	local n1_`outcome' = e(N)
	local effn1_`outcome' = e(N_h_l) + e(N_h_r)

	

*** (2) only cities above 25pct in size

	qui rdrobust res_`outcome' margin_mayor_left if size_above_25pct == 1 & baseline==1, vce(cluster mun_code) all

	local coeff4_`outcome' : di %9.2f (e(tau_bc))
	local se4_`outcome' : di %9.2f (e(se_tau_rb))
	local pv4_`outcome' : di %9.2f (e(pv_rb))
	local n4_`outcome' = e(N)
	local effn4_`outcome' = e(N_h_l) + e(N_h_r)


*** (3) only cities above median in size

	qui rdrobust res_`outcome' margin_mayor_left if size_above_median == 1 & baseline_sample == 1, vce(cluster mun_code) all

	local coeff5_`outcome' : di %9.2f (e(tau_bc))
	local se5_`outcome' : di %9.2f (e(se_tau_rb))
	local pv5_`outcome' : di %9.2f (e(pv_rb))
	local n5_`outcome' = e(N)
	local effn5_`outcome' = e(N_h_l) + e(N_h_r)


*** (4) only cities above 75pct in size

	qui rdrobust res_`outcome' margin_mayor_left if size_above_75pct == 1 & baseline_sample == 1, vce(cluster mun_code) all

	local coeff6_`outcome' : di %9.2f (e(tau_bc))
	local se6_`outcome' : di %9.2f (e(se_tau_rb))
	local pv6_`outcome' : di %9.2f (e(pv_rb))
	local n6_`outcome' = e(N)
	local effn6_`outcome' = e(N_h_l) + e(N_h_r)



*** (5) only cities above 90pct in size

	qui rdrobust res_`outcome' margin_mayor_left if size_above_90pct == 1 & baseline_sample == 1, vce(cluster mun_code) all

	local coeff7_`outcome' : di %9.2f (e(tau_bc))
	local se7_`outcome' : di %9.2f (e(se_tau_rb))
	local pv7_`outcome' : di %9.2f (e(pv_rb))
	local n7_`outcome' = e(N)
	local effn7_`outcome' = e(N_h_l) + e(N_h_r)


}


* write table
texdoc init "${root}/results/tables/table_by_city_size.tex", replace force

tex \caption{RD estimates of the effect of a left-wing mayor - by city size}

tex \resizebox{\linewidth}{!}{
tex \begin{tabularx}{\linewidth}{l *5{>{\Centering}X}}
tex \toprule

tex 													&        Baseline 				& \multicolumn{4}{c}{Subsamples: Population higher than ...} 								\\
tex \cmidrule(lr){2-2} \cmidrule(lr){3-6}

tex 													& 								& 	25th pct `pop25pct' 			& median `pop50pct'		 								& 75th pct	`pop75pct'						& 90th pct	`pop90pct'					\\
tex \midrule
tex \multicolumn{6}{c}{Size of government: overall revenues and expenses} \\
tex \midrule
tex Expenditure per capita 					& `coeff1_tot_exp_avg_pc'`stars1_tot_exp_avg_pc' 					& `coeff4_tot_exp_avg_pc'`stars4_tot_exp_avg_pc' 					& `coeff5_tot_exp_avg_pc'`stars5_tot_exp_avg_pc' 					& `coeff6_tot_exp_avg_pc'`stars6_tot_exp_avg_pc' 						& `coeff7_tot_exp_avg_pc'`stars7_tot_exp_avg_pc' 			\\
tex  										& (`se1_tot_exp_avg_pc') 											& (`se4_tot_exp_avg_pc') 											& (`se5_tot_exp_avg_pc') 											& (`se6_tot_exp_avg_pc') 												& (`se7_tot_exp_avg_pc') 									\\
tex Expenditure, \% of GDP 					& `coeff1_tot_exp_avg_sgdp'`stars1_tot_exp_avg_sgdp' 				& `coeff4_tot_exp_avg_sgdp'`stars4_tot_exp_avg_sgdp' 		 		& `coeff5_tot_exp_avg_sgdp'`stars5_tot_exp_avg_sgdp' 				& `coeff6_tot_exp_avg_sgdp'`stars6_tot_exp_avg_sgdp' 		 			& `coeff7_tot_exp_avg_sgdp'`stars7_tot_exp_avg_sgdp' 		 	\\
tex  										& (`se1_tot_exp_avg_sgdp') 											& (`se4_tot_exp_avg_sgdp') 											& (`se5_tot_exp_avg_sgdp') 											& (`se6_tot_exp_avg_sgdp') 												& (`se7_tot_exp_avg_sgdp') 										\\
tex Revenue per capita 						& `coeff1_tot_rev_avg_pc'`stars1_tot_rev_avg_pc' 					& `coeff4_tot_rev_avg_pc'`stars4_tot_rev_avg_pc' 					& `coeff5_tot_rev_avg_pc'`stars5_tot_rev_avg_pc' 					& `coeff6_tot_rev_avg_pc'`stars6_tot_rev_avg_pc' 						& `coeff7_tot_rev_avg_pc'`stars7_tot_rev_avg_pc' 			\\
tex  										& (`se1_tot_rev_avg_pc') 											& (`se4_tot_rev_avg_pc') 											& (`se5_tot_rev_avg_pc') 											& (`se6_tot_rev_avg_pc') 												& (`se7_tot_rev_avg_pc') 									\\
tex Revenue, \% of GDP 						& `coeff1_tot_rev_avg_sgdp'`stars1_tot_rev_avg_sgdp' 				& `coeff4_tot_rev_avg_sgdp'`stars4_tot_rev_avg_sgdp' 				& `coeff5_tot_rev_avg_sgdp'`stars5_tot_rev_avg_sgdp' 				& `coeff6_tot_rev_avg_sgdp'`stars6_tot_rev_avg_sgdp' 					& `coeff7_tot_rev_avg_sgdp'`stars7_tot_rev_avg_sgdp' 			\\
tex  										& (`se1_tot_rev_avg_sgdp') 											& (`se4_tot_rev_avg_sgdp') 											& (`se5_tot_rev_avg_sgdp') 											& (`se6_tot_rev_avg_sgdp') 												& (`se7_tot_rev_avg_sgdp') 					 				\\
tex \midrule	
tex \multicolumn{6}{c}{Allocation of resources: budget categories (\% of total expenditure)} \\
tex \midrule
tex Current Expenditure 					& `coeff1_current_exp_avg_share'`stars1_current_exp_avg_share' 		& `coeff4_current_exp_avg_share'`stars4_current_exp_avg_share' 		& `coeff5_current_exp_avg_share'`stars5_current_exp_avg_share' 		& `coeff6_current_exp_avg_share'`stars6_current_exp_avg_share' 			& `coeff7_current_exp_avg_share'`stars7_current_exp_avg_share' 		\\
tex  										& (`se1_current_exp_avg_share') 									& (`se4_current_exp_avg_share') 									& (`se5_current_exp_avg_share') 									& (`se6_current_exp_avg_share') 										& (`se7_current_exp_avg_share') 									\\
tex \multicolumn{6}{l}{of which:} \\
tex \hspace{0.20cm} Personnel 								& `coeff1_personnel_avg_share'`stars1_personnel_avg_share' 			& `coeff4_personnel_avg_share'`stars4_personnel_avg_share' 			& `coeff5_personnel_avg_share'`stars5_personnel_avg_share' 			& `coeff6_personnel_avg_share'`stars6_personnel_avg_share' 				& `coeff7_personnel_avg_share'`stars7_personnel_avg_share' 		\\
tex  										& (`se1_personnel_avg_share') 										& (`se4_personnel_avg_share') 										& (`se5_personnel_avg_share') 										& (`se6_personnel_avg_share') 											& (`se7_personnel_avg_share') 									\\
tex Public Investment 						& `coeff1_investments_avg_share'`stars1_investments_avg_share' 		& `coeff4_investments_avg_share'`stars4_investments_avg_share' 		& `coeff5_investments_avg_share'`stars5_investments_avg_share' 		& `coeff6_investments_avg_share'`stars6_investments_avg_share' 			& `coeff7_investments_avg_share'`stars7_investments_avg_share' 		\\
tex  										& (`se1_investments_avg_share') 									& (`se4_investments_avg_share') 									& (`se5_investments_avg_share') 									& (`se6_investments_avg_share') 										& (`se7_investments_avg_share') 									\\
tex \midrule	
tex \multicolumn{6}{c}{Allocation of resources: functional categories (\% of total expenditure)} \\
tex \midrule
tex Social Expenditures 					& `coeff1_social_exp_avg_share'`stars1_social_exp_avg_share' 	& `coeff4_social_exp_avg_share'`stars4_social_exp_avg_share' 	& `coeff5_social_exp_avg_share'`stars5_social_exp_avg_share' 	& `coeff6_social_exp_avg_share'`stars6_social_exp_avg_share' 	& `coeff7_social_exp_avg_share'`stars7_social_exp_avg_share' 		\\
tex  										& (`se1_social_exp_avg_share') 									& (`se4_social_exp_avg_share') 						 			& (`se5_social_exp_avg_share') 						 			& (`se6_social_exp_avg_share') 						 			& (`se7_social_exp_avg_share') 						\\
tex \multicolumn{6}{l}{of which:} \\
tex \hspace{0.20cm} Health \& sanitation 	& `coeff1_health_avg_share'`stars1_health_avg_share' 			& `coeff4_health_avg_share'`stars4_health_avg_share' 			& `coeff5_health_avg_share'`stars5_health_avg_share' 			& `coeff6_health_avg_share'`stars6_health_avg_share' 			& `coeff7_health_avg_share'`stars7_health_avg_share' 			\\
tex  										& (`se1_health_avg_share') 										& (`se4_health_avg_share') 										& (`se5_health_avg_share') 										& (`se6_health_avg_share') 										& (`se7_health_avg_share') 						\\
tex \hspace{0.20cm} Education \& culture 	& `coeff1_education_avg_share'`stars1_education_avg_share' 		& `coeff4_education_avg_share'`stars4_education_avg_share' 		& `coeff5_education_avg_share'`stars5_education_avg_share' 		& `coeff6_education_avg_share'`stars6_education_avg_share' 		& `coeff7_education_avg_share'`stars7_education_avg_share' 		\\
tex  										& (`se1_education_avg_share') 									& (`se4_education_avg_share') 									& (`se5_education_avg_share') 									& (`se6_education_avg_share') 									& (`se7_education_avg_share') 						\\
tex \hspace{0.20cm} Social welfare 			& `coeff1_welfare_avg_share'`stars1_welfare_avg_share' 			& `coeff4_welfare_avg_share'`stars4_welfare_avg_share' 			& `coeff5_welfare_avg_share'`stars5_welfare_avg_share' 			& `coeff6_welfare_avg_share'`stars6_welfare_avg_share' 			& `coeff7_welfare_avg_share'`stars7_welfare_avg_share' 			\\
tex  										& (`se1_welfare_avg_share') 									& (`se4_welfare_avg_share') 									& (`se5_welfare_avg_share') 									& (`se6_welfare_avg_share') 									& (`se7_welfare_avg_share') 						\\
tex \multicolumn{6}{l}{Other expenditures:} \\
tex  \hspace{0.20cm} Housing 				& `coeff1_housing_avg_share'`stars1_housing_avg_share' 			& `coeff4_housing_avg_share'`stars4_housing_avg_share' 			& `coeff5_housing_avg_share'`stars5_housing_avg_share' 			& `coeff6_housing_avg_share'`stars6_housing_avg_share' 			& `coeff7_housing_avg_share'`stars7_housing_avg_share' 			\\
tex  										& (`se1_housing_avg_share') 									& (`se4_housing_avg_share') 									& (`se5_housing_avg_share') 									& (`se6_housing_avg_share') 									& (`se7_housing_avg_share') 						\\
tex  \hspace{0.20cm} Transportation 		& `coeff1_transport_avg_share'`stars1_transport_avg_share' 		& `coeff4_transport_avg_share'`stars4_transport_avg_share' 		& `coeff5_transport_avg_share'`stars5_transport_avg_share' 		& `coeff6_transport_avg_share'`stars6_transport_avg_share' 		& `coeff7_transport_avg_share'`stars7_transport_avg_share' 		\\
tex  										& (`se1_transport_avg_share') 									& (`se4_transport_avg_share') 									& (`se5_transport_avg_share') 									& (`se6_transport_avg_share') 									& (`se7_transport_avg_share') 						\\
tex  \hspace{0.20cm} Other 					& `coeff1_other_avg_share'`stars1_other_avg_share' 				& `coeff4_other_avg_share'`stars4_other_avg_share' 				& `coeff5_other_avg_share'`stars5_other_avg_share' 				& `coeff6_other_avg_share'`stars6_other_avg_share' 				& `coeff7_other_avg_share'`stars7_other_avg_share' 			\\
tex  										& (`se1_other_avg_share') 										& (`se4_other_avg_share') 										& (`se5_other_avg_share') 										& (`se6_other_avg_share') 										& (`se7_other_avg_share') 						\\
tex \midrule

tex Social Expenditures per capita 			& `coeff1_social_exp_avg_pc'`stars1_social_exp_avg_pc' 			& `coeff4_social_exp_avg_pc'`stars4_social_exp_avg_pc' 			& `coeff5_social_exp_avg_pc'`stars5_social_exp_avg_pc' 			& `coeff6_social_exp_avg_pc'`stars6_social_exp_avg_pc' 			& `coeff7_social_exp_avg_pc'`stars7_social_exp_avg_pc' 			\\
tex  										& (`se1_social_exp_avg_pc') 									& (`se4_social_exp_avg_pc') 						 			& (`se5_social_exp_avg_pc') 						 			& (`se6_social_exp_avg_pc') 						 			& (`se7_social_exp_avg_pc') 						\\
tex \bottomrule
tex Observations (all) 						&  `n1_tot_exp_avg_pc'    											& `n4_tot_exp_avg_pc'  												& `n5_tot_exp_avg_pc'  												& `n6_tot_exp_avg_pc'  													& `n7_tot_exp_avg_pc'  											\\
tex Observations (effective)				&  `effn1_tot_exp_avg_pc'											& `effn4_tot_exp_avg_pc'											& `effn5_tot_exp_avg_pc'   											& `effn6_tot_exp_avg_pc'												& `effn7_tot_exp_avg_pc'   										\\
tex \bottomrule
tex \end{tabularx}}

texdoc close
